﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Web4Comment.Common;
namespace UnlessComment.DAL
{
    /// <summary>
    /// 数据访问类:UserInfo
    /// </summary>
    public partial class UserInfo
    {
        public UserInfo()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("user_id", "UserInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from UserInfo");
            strSql.Append(" where user_id=@user_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)			};
            parameters[0].Value = user_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(UnlessComment.Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into UserInfo(");
            strSql.Append("user_id,screen_name,sex,location,hobby,school,company)");
            strSql.Append(" values (");
            strSql.Append("@user_id,@screen_name,@sex,@location,@hobby,@school,@company)");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@screen_name", SqlDbType.VarChar,255),
					new SqlParameter("@sex", SqlDbType.VarChar,1),
					new SqlParameter("@location", SqlDbType.VarChar,255),
					new SqlParameter("@hobby", SqlDbType.VarChar,255),
					new SqlParameter("@school", SqlDbType.VarChar,255),
					new SqlParameter("@company", SqlDbType.VarChar,255)};
            parameters[0].Value = model.user_id;
            parameters[1].Value = model.screen_name;
            parameters[2].Value = model.sex;
            parameters[3].Value = model.location;
            parameters[4].Value = model.hobby;
            parameters[5].Value = model.school;
            parameters[6].Value = model.company;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UnlessComment.Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserInfo set ");
            strSql.Append("screen_name=@screen_name,");
            strSql.Append("sex=@sex,");
            strSql.Append("location=@location,");
            strSql.Append("hobby=@hobby,");
            strSql.Append("school=@school,");
            strSql.Append("company=@company");
            strSql.Append(" where user_id=@user_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@screen_name", SqlDbType.VarChar,255),
					new SqlParameter("@sex", SqlDbType.VarChar,1),
					new SqlParameter("@location", SqlDbType.VarChar,255),
					new SqlParameter("@hobby", SqlDbType.VarChar,255),
					new SqlParameter("@school", SqlDbType.VarChar,255),
					new SqlParameter("@company", SqlDbType.VarChar,255),
					new SqlParameter("@user_id", SqlDbType.Int,4)};
            parameters[0].Value = model.screen_name;
            parameters[1].Value = model.sex;
            parameters[2].Value = model.location;
            parameters[3].Value = model.hobby;
            parameters[4].Value = model.school;
            parameters[5].Value = model.company;
            parameters[6].Value = model.user_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int user_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserInfo ");
            strSql.Append(" where user_id=@user_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)			};
            parameters[0].Value = user_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string user_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserInfo ");
            strSql.Append(" where user_id in (" + user_idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UnlessComment.Model.UserInfo GetModel(int user_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 user_id,screen_name,sex,location,hobby,school,company from UserInfo ");
            strSql.Append(" where user_id=@user_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)			};
            parameters[0].Value = user_id;

            UnlessComment.Model.UserInfo model = new UnlessComment.Model.UserInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["user_id"] != null && ds.Tables[0].Rows[0]["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["screen_name"] != null && ds.Tables[0].Rows[0]["screen_name"].ToString() != "")
                {
                    model.screen_name = ds.Tables[0].Rows[0]["screen_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sex"] != null && ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["location"] != null && ds.Tables[0].Rows[0]["location"].ToString() != "")
                {
                    model.location = ds.Tables[0].Rows[0]["location"].ToString();
                }
                if (ds.Tables[0].Rows[0]["hobby"] != null && ds.Tables[0].Rows[0]["hobby"].ToString() != "")
                {
                    model.hobby = ds.Tables[0].Rows[0]["hobby"].ToString();
                }
                if (ds.Tables[0].Rows[0]["school"] != null && ds.Tables[0].Rows[0]["school"].ToString() != "")
                {
                    model.school = ds.Tables[0].Rows[0]["school"].ToString();
                }
                if (ds.Tables[0].Rows[0]["company"] != null && ds.Tables[0].Rows[0]["company"].ToString() != "")
                {
                    model.company = ds.Tables[0].Rows[0]["company"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select user_id,screen_name,sex,location,hobby,school,company ");
            strSql.Append(" FROM UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" user_id,screen_name,sex,location,hobby,school,company ");
            strSql.Append(" FROM UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.user_id desc");
            }
            strSql.Append(")AS Row, T.*  from UserInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "UserInfo";
            parameters[1].Value = "user_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

